<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("Writer.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Vehicle_Misc_List extends Page
{
       public $hfForm = null;
       public $JSWindowOpenNewMixed = null;
       public $hfType = null;
       public $JTJavaScript2 = null;
       public $JTMiscHidden = null;
       public $JTPlatinumVehicleMisc = null;
       public $windowOpen = null;
       public $JTJavaScript1 = null;
       public $hiddenWO = null;
       public $JTSiteTheme1 = null;
       public $hiddenId = null;
       function edtSearchJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('edtSearch').value = '';
       <?php

       }

       function btnSearchClick($sender, $params)
       {
       redirect('veh_parts_list.php?veh='.$this->hiddenId->getValue().'&WO='.$this->hiddenWO->getValue());


       }

       function btnExportClick($sender, $params)
       {
       $this->JTPartsHidden->exportGridToXLSDownload('parts_list.xls');


       }

       function JTMiscHiddenShow($sender, $params)
       {
       $veh_id = $this->hiddenId->getValue();

       $query_veh = "select * from vehicle_tbl where vehicle_id='".$this->hiddenId->getValue()."'";
       $result_veh = mysql_query($query_veh)or die(mysql_error());
       $row_veh = mysql_fetch_array($result_veh);

       $query1 = "select * from veh_misc_tbl where vehicle_id='".$veh_id."' and workorder='".$this->hiddenWO->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());

       $arr = array();

       $arr[] = array(
                        '1' => 'Vehicle Num:'.' '.$row_veh['VIN'],
                        '2'=>' ',
                        '3' => ' '
                    );


       $arr[] = array(
                        '1' => 'Plate Num:'.' '.$row_veh['veh_num'],
                        '2'=> ' ',
                        '3' => ' '
                    );

       $arr[] = array(
                        '1' => 'Year:'.' '.$row_veh['year'],
                        '2'=>' ',
                        '3' => ' '
                    );

       $arr[] = array(
                        '1' => 'Make:'.' '.$row_veh['manufacturer_name'],
                        '2'=>' ',
                        '3' => ' '
                    );

       $arr[] = array(
                        '1' => 'Model:'.' '.$row_veh['model_name'],
                        '2'=> ' ',
                        '3' => ' '
                    );

       $arr[] = array(
                        '1' => 'Type/Style:'.' '.$row_veh['style_name'],
                        '2'=> ' ',
                        '3' => ' '
                    );

       $arr[] = array(
                        '1' => ' ',
                        '2'=>' ',
                        '3' => ' '
                    );


       $arr[] = array(
                        '1' => 'WORKORDER - MISC REPORT',
                        '2'=>' ',
                        '3' => ' '
                    );

      $arr[] = array(
      '1' => 'Date',
      '2'=> 'Description',
      '3' => 'Amount'
      );



       while($row = mysql_fetch_array($result))
       {

       $quantity = "<a href=veh_misc_edit.php?veh=".$veh_id."&WO=".$this->hiddenWO->getValue()."&misc=".$row['misc_id'].">".$row['date']."</a>";
       $tools2 = "<img onclick=js_deleteParts('".$veh_id."','".$this->hiddenWO->getValue()."','".$row['misc_id']."'); src='image/delete_orange.jpg' title='delete'>";

       $arr[] = array(
                        '1' => $row['date'],
                        '2' => $row['description'],
                        '3' => $row['amount']
                        );
       }

       $total_amount = mysql_query("SELECT SUM(amount) as sum_amount FROM veh_misc_tbl where vehicle_id='".$this->hiddenId->getValue()."' and workorder='".$this->hiddenWO->getValue()."'");
       $row_amount = mysql_fetch_array($total_amount);

        $arr[] = array(
                        '1' => TOTAL,
                        '3' => $row_amount['sum_amount']
                        );

        $this->JTMiscHidden->setCellData($arr);


       }

       function imgAddPartsJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgAddParts'].src="image/add_new.png"
       <?php

       }

       function imgAddPartsJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgAddParts'].src="image/add_new_blue.png"
       <?php

       }

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }


       function commafy($_)
       {
        return strrev( (string)preg_replace( '/(\d{3})(?=\d)(?!\d*\.)/', '$1,' , strrev( $_ ) ) );
       }



       function Vehicle_Misc_ListCreate($sender, $params)
       {
       if($_SESSION['isAdmin']!=1)
       {
        $this->imgAddParts->setVisible(false);
        $this->btnExport->Left = 952;
       }

       $id = $_GET['veh'];
       $parts = $_GET['WO'];
       $type = $_GET['type'];
       $form = $_GET['form'];
       $this->hiddenId->setValue($id);
       $this->hiddenWO->setValue($parts);
       $this->hfType->setValue($type);
       $this->hfForm->setValue($form);

       $result_order = mysql_query("select * from veh_workorder_tbl where workorder_id='".$this->hiddenWO->getValue()."'");
       $row_order = mysql_fetch_array($result_order);
       //$this->lblOdernum->Caption=$row_order['order_num'];

       $result_vin = mysql_query("SELECT * FROM vehicle_tbl WHERE vehicle_id='".$this->hiddenId->getValue()."'")or die(mysql_error());
       $row_selected = mysql_fetch_array($result_vin);

       echo '<script language=javascript>
       window.parent.setWODetails();
       </script>';
       /*$label = $row_selected['VIN'];
       $this->lblVeh->Caption = $label;
       $this->lblyr->Caption = $row_selected['year'];
       $this->lblmake->Caption = $row_selected['manufacturer_name'];
       $this->lblmodel->Caption = $row_selected['model_name'];
       $this->lblPlateNum->Caption = $row_selected['veh_num'];


       $this->imgAddParts->setLink('veh_parts_create.php?veh='.$this->hiddenId->getValue().'&WO='.$this->hiddenWO->getValue());
       $this->imgBack->setLink('veh_workorder_list.php?veh='.$this->hiddenId->getValue().'&WO'.$this->hiddenWO->getValue());*/
       }

       function JTPlatinumVehicleMiscShow($sender, $params)
       {
       $veh_id = $this->hiddenId->getValue();
       /*$query1 = "select * from veh_parts_tbl where vehicle_id='".$veh_id."' and workorder='".$this->hiddenWO->getValue()."' and
       (part_num like '%".$this->edtSearch->Text."%'
       or quantity like '%".$this->edtSearch->Text."%'
       or price_ea like '%".$this->edtSearch->Text."%'
       or amount like '%".$this->edtSearch->Text."%')";
       $result = mysql_query($query1)or die(mysql_error());*/


       $query1 = "select * from veh_misc_tbl where vehicle_id='".$veh_id."' and workorder='".$this->hiddenWO->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());

       $arr = array();
       while($row = mysql_fetch_array($result))
       {
       //$quantity = "<a href=veh_parts_edit.php?veh=".$veh_id."&WO=".$this->hiddenWO->getValue()."&parts=".$row['parts_id'].">".$row['part_num']."</a>";

       /*if($this->hfType->getValue()=='edit')
       {
       $endorsement = "<span onclick=openEndorsement(".$row['misc_id']."); style='color:blue; cursor:pointer; text-decoration: underline;'>".$row['date']."</span>";
       }
       if($this->hfType->getValue()=='create')
       {
       $endorsement = "<span onclick=openEditMisc(".$row['misc_id']."); style='color:blue; cursor:pointer; text-decoration: underline;'>".$row['date']."</span>";
       }*/


       //REDUNDANT
       if($this->hfType->getValue()=='edit')
       {
       $type = 'edit';
       }
       if($this->hfType->getValue()=='create')
       {
       $type = 'create';
       }

       $endorsement = "<span onclick=openEditMisc2('".$row['misc_id']."','".$type."'); style='color:blue; cursor:pointer; text-decoration: underline;'>".$row['date']."</span>";

       //FOR ENDORSEMENT
       $query_endo = "select * from endorsement_misc_tbl where veh_id='".$this->hiddenId->getValue()."' and wo_id='".$this->hiddenWO->getValue()."' and misc_id='".$row['misc_id']."'";
       $result_endo = mysql_query($query_endo)or die(mysql_error());
       $row_endo = mysql_fetch_array($result_endo);
       $endo_count = mysql_num_rows($result_endo);

       if($_SESSION['access_vehicle']==1)
       {
       if($this->hfForm->getValue()=='summary')
       {
       if($endo_count!=0)
       {
       $tools2 = "<img onclick=window.open('misc_endorsement_list.php?veh=".$this->hiddenId->getValue()."&WO=".$this->hiddenWO->getValue()."&misc=".$row['misc_id']."','popup','width=688,height=344,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=300,top=200'); src='image/endo.png' title='View Endorsements' style='cursor:pointer'>";
       }

       if($endo_count==0)
       {
       $tools2 = "";
       }
       }

       if($this->hfForm->getValue()=='order')
       {
       $tools2 = "";
       }
       }

       if($_SESSION['access_vehicle']==2)
       {
       if($this->hfForm->getValue()=='summary')
       {
       if($endo_count!=0)
       {
       $tools2 = "<img onclick=js_deleteParts('".$veh_id."','".$this->hiddenWO->getValue()."','".$row['misc_id']."','".$this->hfType->getValue()."','".$this->hfForm->getValue()."'); src='image/delete.png' title='delete' style='cursor:pointer'>
       <img onclick=window.open('misc_endorsement_list.php?veh=".$this->hiddenId->getValue()."&WO=".$this->hiddenWO->getValue()."&misc=".$row['misc_id']."','popup','width=688,height=344,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=300,top=200'); src='image/endo.png' title='View Endorsements' style='cursor:pointer'>";
       }

       if($endo_count==0)
       {
       $tools2 = "<img onclick=js_deleteParts('".$veh_id."','".$this->hiddenWO->getValue()."','".$row['misc_id']."','".$this->hfType->getValue()."','".$this->hfForm->getValue()."'); src='image/delete.png' title='delete' style='cursor:pointer'>";
       }
       }

       if($this->hfForm->getValue()=='order')
       {
       $tools2 = "<img onclick=js_deleteParts('".$veh_id."','".$this->hiddenWO->getValue()."','".$row['misc_id']."','".$this->hfType->getValue()."','".$this->hfForm->getValue()."'); src='image/delete.png' title='delete' style='cursor:pointer'>";
       }
       }

       //END FOR ENDORSEMENT


       //$endorsement = "<span onclick=openEndorsement(".$row['misc_id']."); style='color:blue; cursor:pointer; text-decoration: underline;'>".$row['date']."</span>";
       //$quantity = "<span onclick=openEditMisc(".$row['misc_id']."); style='color:blue; cursor:pointer; text-decoration: underline;'>".$row['date']."</span>";


       $amt = $row['amount'];
       $camt = $this->commafy($amt);

       if($_SESSION['isAdmin']==1)
       {
       $arr[] = array(
                        'Date' => $endorsement,
                        'Description' => $row['description'],
                        'Amount' => $camt,
                        'Tools' => $tools2
                        );
       }
       if($_SESSION['isAdmin']!=1)
       {
       $arr[] = array(
                        'Date' => $quantity,
                        'Description' => $row['description'],
                        'Amount' => $camt
                        );
       }

       }

       $total_amount = mysql_query("SELECT SUM(amount) as sum_amount FROM veh_misc_tbl where vehicle_id='".$this->hiddenId->getValue()."' and workorder='".$this->hiddenWO->getValue()."'");
       $row_amount = mysql_fetch_array($total_amount);

       $tot_amt = $row_amount['sum_amount'];
       $ctot_amt = $this->commafy($tot_amt);

        $arr[] = array(
                        'Date' => TOTAL,
                        'Amount' => $ctot_amt
                        );

        $this->JTPlatinumVehicleMisc->setCellData($arr);
       }

}

global $application;

global $Vehicle_Misc_List;

//Creates the form
$Vehicle_Misc_List=new Vehicle_Misc_List($application);

//Read from resource file
$Vehicle_Misc_List->loadResource(__FILE__);

//Shows the form
$Vehicle_Misc_List->show();

?>